﻿using USClientKernel.MVC;
using Game.Logic.Manager;
using GameDataType = Game.Logic.Data.GameDataType;

namespace Game.Kernel
{
    public abstract class GameBaseData : BaseData<GameBaseData>
    {
        private GameDataType m_dataType = GameDataType.Invalid;
        public GameDataType DataType { get => m_dataType; }
        protected void SetDataType(GameDataType _dataType) { m_dataType = _dataType; }

        private int m_dataIndex;
        public int DataIndex { get => m_dataIndex; }
        public void SetDataIndex(int _dataIndex) { m_dataIndex = _dataIndex; }

        public override void InsertChild(GameBaseData _data)
        {
            _data.Parent = this;
            Children.Add(_data);
        }

        public override void RemoveChild(GameBaseData _data)
        {
            Children.Remove(_data);
            _data.Parent = null;
        }

        public override void RemoveChildren()
        {
            Children.Clear();
        }

        public override void RemoveFromParent()
        {
            Children.Clear();
            Parent.Children.Remove(this);
            Parent = null;
        }

        public override void DispatchDataEvent(GameBaseData _data)
        {
            base.DispatchDataEvent(_data);
        }

        public static T Create<T>(GameDataType _dataType) where T : GameBaseData, new()
        {
            T gameData = new T();
            gameData.m_dataType = _dataType;
            GameDataManager.Instance.RegisterGameData(gameData);
            return gameData;
        }
    }
}
